package ua.kpi_java_training.tyshchenko.module1;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests for Division.class
 *
 * @version 1.1 25 May 2013
 * @author Vlad Tyshchenko
 */
public class DivisionTest {
    Division testDiv = new Division();

    @Test
    public void testGetSign() {
        testDiv.dividend = -100;
        testDiv.divisor = 5;
        Assert.assertEquals("one numbers is negative, one numbers is positive", testDiv.getSign(), -1);
        testDiv.dividend = -100;
        testDiv.divisor = -5;
        Assert.assertEquals("two numbers is negative",testDiv.getSign(), 1);
        testDiv.dividend = 100;
        testDiv.divisor = -5;
        Assert.assertEquals("one numbers is negative, one numbers is positive", testDiv.getSign(), -1);
        testDiv.dividend = 100;
        testDiv.divisor = 5;
        Assert.assertEquals("two numbers is positive", testDiv.getSign(), 1);
    }

    @Test
    public void testGetAmountDigit() {
        Assert.assertEquals("amount '3'", testDiv.getAmountDigit(3), 1);
        Assert.assertEquals("amount '100'", testDiv.getAmountDigit(100), 3);
        Assert.assertEquals("amount '123456'", testDiv.getAmountDigit(123456), 6);
    }

    @Test
    public void testGetIncreasePositiveNumbers() {
        testDiv.dividend = 12316;
        testDiv.divisor = 21;
        testDiv.amountDigitDividend = testDiv.getAmountDigit(testDiv.dividend);
        testDiv.amountDigitDivisor = testDiv.getAmountDigit(testDiv.divisor);
        testDiv.dividendInsideIteration = 12;
        Assert.assertEquals("increase in first iteration", testDiv.getIncrease(0), 3);
        testDiv.checkDividendInsideIteration(testDiv.getIncrease(0));
        Assert.assertEquals("increase in second iteration", testDiv.getIncrease(0), 1);
        Assert.assertEquals("increase in third iteration", testDiv.getIncrease(1), 6);
    }

    @Test
    public void testGetIncreaseNegativeNumbers() {
        testDiv.dividend = -601;
        testDiv.divisor = 5;
        testDiv.amountDigitDividend = testDiv.getAmountDigit(testDiv.dividend);
        testDiv.amountDigitDivisor = testDiv.getAmountDigit(testDiv.divisor);
        testDiv.dividendInsideIteration = 6;
        Assert.assertEquals("increase in first iteration", testDiv.getIncrease(0), 0);
        testDiv.checkDividendInsideIteration(testDiv.getIncrease(0));
        Assert.assertEquals("increase in second iteration", testDiv.getIncrease(1), 1);
    }


    @Test
    public void testCheckDividendInsideIterationTrue() {
        testDiv.dividend = 12316;
        testDiv.divisor = 21;
        double increase = 0;
        testDiv.amountDigitDividend = 5;
        testDiv.amountDigitDivisor = 2;
        testDiv.dividendInsideIteration = Math.abs(testDiv.dividend)
                / (int) Math.pow(10, (testDiv.amountDigitDividend - testDiv.amountDigitDivisor));
        increase = testDiv.getIncrease(0);
        testDiv.checkDividendInsideIteration(increase);
        Assert.assertEquals("dividendInsideIteration < divisor", testDiv.dividendInsideIteration, 123);
    }

    @Test
    public void testCheckDividendInsideIterationFalse() {
        testDiv.dividend = 12316;
        testDiv.divisor = 11;
        double increase = 0;
        testDiv.amountDigitDividend = 5;
        testDiv.amountDigitDivisor = 2;
        testDiv.dividendInsideIteration = Math.abs(testDiv.dividend)
                / (int) Math.pow(10, (testDiv.amountDigitDividend - testDiv.amountDigitDivisor));
        increase = testDiv.getIncrease(0);
        testDiv.checkDividendInsideIteration(increase);
        Assert.assertEquals("dividendInsideIteration > divisor", testDiv.dividendInsideIteration, 12);
    }

    @Test
    public void testGetFirstResultsPositiveNumbers() {
        testDiv.dividend = 12316;
        testDiv.divisor = 21;
        testDiv.getFirstResults();
        Assert.assertEquals("first result positive numbers(dividendInsideIteration < divisor)", testDiv.multiply, 105);
        testDiv.dividend = 601;
        testDiv.divisor = 5;
        testDiv.getFirstResults();
        Assert.assertEquals("first result positive numbers(dividendInsideIteration > divisor)", testDiv.multiply, 5);
    }

    @Test
    public void testGetFirstResultsNegativeNumbers() {
        testDiv.dividend = -601;
        testDiv.divisor = 5;
        testDiv.getFirstResults();
        Assert.assertEquals("first result negative numbers", testDiv.multiply, -5);
    }

    @Test
    public void testNextDividendInsideIteration() {
        testDiv.dividend = 12316;
        testDiv.divisor = 21;
        testDiv.getFirstResults();
        int increase = testDiv.getIncrease(0);
        testDiv.dividendInsideIteration = testDiv.getNextDividendInsideIteration(0, increase);
        Assert.assertEquals("first devidend ", testDiv.dividendInsideIteration, 181);
        testDiv.getFirstResults();
        testDiv.getNextResults(0);
        increase = testDiv.getIncrease(1);
        testDiv.dividendInsideIteration = testDiv.getNextDividendInsideIteration(1, increase);
        Assert.assertEquals("second dividend ", testDiv.dividendInsideIteration, 136);
    }

    @Test
    public void testGetNextResultsPositiveNumbers() {
        testDiv.dividend = 12316;
        testDiv.divisor = 21;
        testDiv.getFirstResults();
        testDiv.getNextResults(0);
        Assert.assertEquals("first multiplication ", testDiv.multiply, 168);
        testDiv.getNextResults(1);
        Assert.assertEquals("second multiplication ", testDiv.multiply, 126);
    }

    @Test
    public void testGetNextResultsNegativeNumbers() {
        testDiv.dividend = -12316;
        testDiv.divisor = 21;
        testDiv.getFirstResults();
        testDiv.getNextResults(0);
        Assert.assertEquals("first multiplication ", testDiv.multiply, -168);
        testDiv.getNextResults(1);
        Assert.assertEquals("second multiplication ", testDiv.multiply, -126);
    }
}
